//==============================================================================
//
// File: CInternetList.cpp
// Project: Browser
// Author(s): Matthias Van Eeghem (matthias@van-eeghem.com)
// License: See LICENSE in root directory
//
// This class holds all methods related to the internet list. This list shows all
// currently online servers for the mod.
//
//==============================================================================

#include "CInternetList.h"
#include "ui_MainWindow.h"
#include "Constants.h"

MainWindow * CInternetList::m_pMainWindow;

// This method initialises the InternetList treeview
void CInternetList::InitialiseInternetList(MainWindow * parent)
{
    m_pMainWindow = parent;

    // Set the column widths
    parent->ui->InternetList->setColumnWidth(0, 200);
    parent->ui->InternetList->setColumnWidth(2, 150);

    // Set a custom context menu
    parent->ui->InternetList->addAction(parent->ui->actionConnect);
    parent->ui->InternetList->addAction(parent->ui->actionAddToFavoriteList);
    parent->ui->InternetList->addAction(parent->ui->actionRefresh_internet_list);
    parent->ui->InternetList->setContextMenuPolicy(Qt::ActionsContextMenu);
}

// This method handles the reply to the masterlist request
void CInternetList::HandleInternetListReply(QString sContents)
{
    // Get all servers as an element in the QStringList
    QStringList ServerList = sContents.split(SERVER_SEPERATOR);

    // Loop through all the servers
    for (int i = 0; i < ServerList.size(); ++i)
    {
        // Get the current server string
        QString ServerString = ServerList.at(i);

        // Make sure the server is valid
        if(ServerString.length() > 1)
        {   
            // Split the server ip into the IP and port
            QStringList ServerInformation = ServerString.split(SERVERDATA_SEPERATOR);
            QString ServerIP = ServerInformation[0].split(':')[0];
            QString ServerPort = ServerInformation[0].split(':')[1];

            // Setup the CServer object
            CServer Server;
            Server.m_sIP = ServerIP;
            Server.m_sPort = ServerPort;
            Server.m_sVersion = ServerInformation[1];
            Server.m_sHostname = ServerInformation[2];
            Server.m_iPlayers = ServerInformation[3].toInt();
            Server.m_iMaxPlayers = ServerInformation[4].toInt();

            // Add the CServer object to the QList
            m_pMainWindow->m_lInternetList.append(Server);

            // Add the item to the internet list
            QTreeWidgetItem * Item = new QTreeWidgetItem(m_pMainWindow->ui->InternetList);
            Item->setText(COLUMN_HOSTNAME, Server.m_sHostname);
            Item->setText(COLUMN_PLAYERS, ServerInformation[3] + "/" + ServerInformation[4]);
            Item->setText(COLUMN_VERSION, Server.m_sVersion);
            Item->setText(COLUMN_IP, Server.m_sIP + ":" + Server.m_sPort);
            m_pMainWindow->ui->InternetList->addTopLevelItem(Item);

            // We now check if we have any of the servers on the online list also on the favorites list
            // If so, we add the CServer object to the favorite server as well and update the TreeViewItem

            // Get the full ip (ip:port)
            QString FullIP = (ServerIP + ':' + ServerPort);

            // We loop through all the favorite servers
            for(int i = 0; i < m_pMainWindow->m_lFavoritesList.size(); i++)
            {
                // Check if we have a match
                if(m_pMainWindow->m_lFavoritesList.at(i).m_sHostname == FullIP)
                {
                    // Replace the (invalid) CServer object of the favorite server with a valid CServer object
                    m_pMainWindow->m_lFavoritesList.replace(i, Server);

                    // Update the TreeView item
                    m_pMainWindow->ui->FavoritesList->topLevelItem(i)->setText(COLUMN_HOSTNAME, Server.m_sHostname);
                    m_pMainWindow->ui->FavoritesList->topLevelItem(i)->setText(COLUMN_PLAYERS, ServerInformation[3] + "/" + ServerInformation[4]);
                    m_pMainWindow->ui->FavoritesList->topLevelItem(i)->setText(COLUMN_VERSION, Server.m_sVersion);
                    m_pMainWindow->ui->FavoritesList->topLevelItem(i)->setText(COLUMN_IP, Server.m_sIP + ":" + Server.m_sPort);
                }
            }
        }
    }
}
